#include<bits/stdc++.h>
using namespace std;
typedef long long van;
template<typename T> inline
void read(T& x) {
    T f = 1, b = 0; char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    } while (isdigit(ch))
        b *= 10, b += ch - '0', ch = getchar();
    x = f * b; return;
}
template<typename T> inline
void print(T x) {
    if (x == 0) return putchar('0'), void();
    if (x < 0) putchar('-'), x = -x;
    int st[129] = {0}, k = 0;
    while (x) st[++k] = x % 10, x /= 10;
    for (int i = k; i; i--) putchar(st[i] + '0');
} const van MaxN = 1e5 + 10;
van n, ans = 1e18;
struct node {
    van a, b;
}val[MaxN];
van preab[MaxN], prea[MaxN];
van sufab[MaxN], sufa[MaxN];
int main() {
    freopen("boast.in", "r", stdin);
    freopen("boast.out", "w", stdout);

    read(n);
    for (int i = 1; i <= n; i++) read(val[i].a), read(val[i].b);
    sort(val + 1, val + n + 1, [](node a, node b){
        return a.b == b.b ? a.a < b.a : a.b < b.b;
    });
    
    preab[0] = prea[0] = 1e18;
    for (int i = 1; i <= n; i++) preab[i] = min(preab[i - 1], val[i].a + val[i].b), prea[i] = min(prea[i - 1], val[i].a);
    sufab[n + 1] = sufa[n + 1] = 1e18;
    for (int i = n; i >= 1; i--) sufab[i] = min(sufab[i + 1], val[i].a + val[i].b), sufa[i] = min(sufa[i + 1], val[i].a);

    // b1 > b2 && b2 > b3
    for (int i = 2; i < n; i++) {
        van tmp = val[i].a + val[i].b;
        tmp += sufab[i + 1];
        tmp += prea[i - 1];
        ans = min(ans, tmp);
    }

    van mina = min(val[1].a, val[2].a), mina2 = max(val[1].a, val[2].a);
    // b1 < b2 && b2 > b3
    for (int i = 3; i <= n; i++) {
        van tmp = 2 * val[i].b + val[i].a;
        tmp += mina + mina2; ans = min(ans, tmp);
        if (val[i].a >= mina2) continue;
        if (val[i].a >= mina) mina2 = val[i].a;
        else mina2 = mina, mina = val[i].a;
    }

    // b1 > b2 && b2 < b3
    van minab = min(val[n].a + val[n].b, val[n - 1].a + val[n - 1].b),
        minab2 = max(val[n].a + val[n].b, val[n - 1].a + val[n - 1].b);
    for (int i = n - 2; i >= 1; i--) {
        van tmp = val[i].a;
        tmp += minab + minab2; ans = min(ans, tmp);
        van cmpnum = val[i].a + val[i].b;
        if (cmpnum >= minab2) continue;
        if (cmpnum >= minab) minab2 = cmpnum;
        else minab2 = minab, minab = cmpnum;
    }

    print(ans), putchar('\n');
}